﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using KGE.Define;

namespace KGE.BasicCOM
{

    public class TestGameCOM : GameCOM
    {
        Character chr;
        Scene scn;
        public TestGameCOM(Game g)
            : base(g)
        {
            chr = new Character();
            chr.LoadActions(game.Options.RootPath + "character\\fighter-nothing1.kca");
            chr.LoadActions(game.Options.RootPath + "character\\fighter-nothing2.kca");
            chr.LoadActions(game.Options.RootPath + "character\\fighter-walking.kca");
            chr.LoadActions(game.Options.RootPath + "character\\fighter-running.kca");
            //chr.LoadActions("fighter-fist1.kca");
            //chr.LoadActions("fighter-fist2.kca");
            //chr.LoadActions("fighter-fist3.kca");
            //chr.LoadActions("fighter-fist4.kca");
            //chr.LoadActions("fighter-light_kick.kca");

            chr.LoadAvatar(game.Options.RootPath + "character\\fm_body0000.kip", game);
            chr.LoadAvatar(game.Options.RootPath + "character\\fm_pants0000a.kip", game);
            chr.LoadAvatar(game.Options.RootPath + "character\\fm_pants0000b.kip", game);
            chr.LoadAvatar(game.Options.RootPath + "character\\fm_shoes0000b.kip", game);

            scn = (Scene)Util.LoadObject(game.Options.RootPath + "scene\\dungeon-001\\d1.kscene");
        }
        public override void Code()
        {
            base.Code();

            //if (game.KeyboardIsIdle)
            //{
            //    chr.DoNothing();
            //}
            //chr.Execute(game.KeyStreamText);
            chr.Heartbeat();
        }
        public override void Draw()
        {
            base.Draw();

            game.DrawScene(scn);
            game.DrawCharacter(scn,chr);
            game.Devices.GPU.DrawText("FPS: " + game.FPS.ToString("f3") + "\n" + "FPS MAX: " + game.FPSMax.ToString("f3") + "\n" + "FPS MIN: " + game.FPSMin.ToString("f3") + "\n" + game.KeyStreamText, Color.White, new Vector3());
        }
        //void drawCharacter(Character chr)
        //{
        //    foreach (KImage ki in chr.Avatar)
        //    {
        //        Vector3 v3 = Util.Point2Vector3(chr.Location);
        //        game.DrawKImage(ki, Color.White, v3);
        //    }       

        //    //foreach (ObjectIndexer poi in chr.AvatarKIPNames)
        //    //{
        //    //    Vector3 v3 = Util.Point2Vector3(chr.Location);
        //    //    game.DrawKImage(poi, chr.ActionIndex, Color.White, v3);
        //    //}

        //}
        public Point Camera;


        public override void OnKeyUpCode(object sender, Key key)
        {
            //if (key == Key.W || key == Key.A || key == Key.S || key == Key.D)
            {
                if (game.KeyboardIsIdle)
                {
                    chr.DoNothing1();
                }
            }
        }
        public override void OnKeyHoldCode(object sender, Key key)
        {
            if (key == Key.W)
            {
                if (chr.IsNothing)
                {
                    chr.Direction = (Direction.U);
                    chr.DoWalking();
                }
                chr.MoveUp();
            }
            else if (key == Key.S)
            {
                if (chr.IsNothing)
                {
                    chr.Direction = (Direction.D);
                    chr.DoWalking();
                }
                chr.MoveDown();
            }
            else if (key == Key.A)
            {
                if (chr.IsNothing)
                {
                    chr.Direction = (Direction.L);
                    chr.DoWalking();
                }
                chr.MoveLeft();
                scn.Location.X+=2;
            }
            else if (key == Key.D)
            {
                if (chr.IsNothing)
                {
                    chr.Direction = (Direction.R);
                    chr.DoWalking();
                }
                chr.MoveRight();
                scn.Location.X -= 2;
            }
        }
        public override void OnKeyDownCode(object sender, Key key)
        {
            if (key == Key.W)
            {
                chr.Direction = (Direction.U);
                if (chr.IsRunning)
                {
                    chr.DoRunning();
                }
                else
                {
                    chr.DoWalking();
                }
            }
            else if (key == Key.S)
            {
                chr.Direction = (Direction.D);
                if (chr.IsRunning)
                {
                    chr.DoRunning();
                }
                else
                {
                    chr.DoWalking();
                }
            }
            else if (key == Key.A)
            {
                chr.Direction = (Direction.L);
                if (game.KeyStreamText.EndsWith("down-a"))
                {
                    chr.DoRunning();
                }
                else
                {
                    chr.DoWalking();
                }
            }
            else if (key == Key.D)
            {
                chr.Direction = (Direction.R);
                if (game.KeyStreamText.EndsWith("down-d"))
                {
                    chr.DoRunning();
                }
                else
                {
                    chr.DoWalking();
                }
            }

            else if (key == Key.J)
            {
                chr.DoAttack();
            }
            else if (key == Key.F12)
            {
                game.SwitchFW();
            }
            else if (key == Key.Escape)
            {
                game.Exit();
            }
        }
    }
}
